package com.hx.vending.controller;

import com.hx.vending.annotation.Authorization;
import com.hx.vending.bean.Trails;
import com.hx.vending.service.ITrailsService;
import com.hx.vending.util.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * Created by Administrator on 2018/11/26.
 * 货道功能
 */
@Controller
@RequestMapping("/trails")
public class TrailsController {

    @Autowired
    private ITrailsService iTrailsService;


    /**
     * 添加货道信息
     * @param trails
     * @return
     */
    @RequestMapping(value = "/insertTrails", method = RequestMethod.POST)
    @ResponseBody
    public Msg insertTrails(Trails trails, @RequestParam(value = "modelId", required = false)Integer modelId){

        return this.iTrailsService.insertTrails(trails,modelId);
    }

    /**
     * 修改货道信息
     * @param trails
     * @return
     */
    @RequestMapping(value = "/updateTrails", method = RequestMethod.POST)
    @ResponseBody
    public Msg updateTrails(Trails trails, @RequestParam(value = "modelId", required = false)Integer modelId){

        return this.iTrailsService.updateTrails(trails, modelId);
    }


    /**
     * 删除货道信息
     * @param trailsId
     * @return
     */
    @RequestMapping(value = "/deleteTrails", method = RequestMethod.DELETE)
    @ResponseBody
    public Msg deleteTrails(@RequestParam("trailsId") Integer trailsId){

        return this.iTrailsService.deleteTrails(trailsId);
    }


    /**
     * 根据货道id查询补货时所需信息
     * @param trailsId
     * @return
     */
    @GetMapping("/selectReplenishmentMessage")
    @ResponseBody
    public Msg selectReplenishmentMessage(Integer trailsId) {

        return this.iTrailsService.selectReplenishmentMessage(trailsId);
    }


    /**
     * 货道补货（单个）
     * @param goodsId
     * @param trailsId
     * @param goodsNumber
     * @return
     */
    @PostMapping("/updateReplenishmentMessage")
    @ResponseBody
    public Msg updateReplenishmentMessage(String goodsId, Integer trailsId, Integer goodsNumber) {
        return this.iTrailsService.updateReplenishmentMessage(goodsId, trailsId, goodsNumber);
    }

    /**
     * 设备一键补货
     * @param equipmentId
     * @return
     */
    @PostMapping("/updateAllReplenishmentMessage")
    @ResponseBody
    public Msg updateupdateAllReplenishmentMessage(String equipmentId) {

        return this.iTrailsService.updateupdateAllReplenishmentMessage(equipmentId);

    }


    /**
     * 货道故障解决
     * @param trailsId
     * @return
     */
    @RequestMapping(value = "/updateTrailsFaultResolution", method = RequestMethod.POST)
    @ResponseBody
    public Msg updateTrailsFaultResolution(Integer trailsId) {

        return this.iTrailsService.updateTrailsFaultResolution(trailsId);
    }


    /**
     * 货道故障列表
     * @param equipmentId
     * @return
     */
    @RequestMapping("/selectTrailsAlarm")
    @ResponseBody
    public Msg selectTrailsAlarm(String equipmentId) {

        return this.iTrailsService.selectTrailsAlarm(equipmentId);
    }

    /**
     * 修改货道容量
     * @param trails
     * @return
     */
    @RequestMapping("/editTrails")
    @ResponseBody
    @Authorization
    public Msg editTrails(@RequestBody Trails trails){
        trails.setTrailsTrackTime(trails.getTrailsTrackTime() * 10);
        int i = iTrailsService.updateByPrimaryKey(trails);
        if(i > 0){
            return Msg.success();
        }
        return Msg.fail();
    }
}
